"""
Case Type   : xml类型
Case Name   : xml类型作为MOT表字段类型，合理报错
Create At   : 2022/8/29
Owner       : songjing20
Description :
    1.设置enable_incremental_checkpoint参数为off,重启数据库
    2.创建mot表，字段类型包含xml
    3.清理环境
Expect      :
    1.设置成功，重启数据库成功
    2.mot表不支持xml类型，合理报错
    3.成功
History     :
    modified by nanyang12 2025/3/18: 研发代码变更，增加motserver开关配置
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class XmlDatatype(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.table = 't_xml_0006'
        self.param = "enable_incremental_checkpoint"

    def test_xml_datatype(self):
        text = '-----step1:设置enable_incremental_checkpoint参数为off,' \
               '重启数据库;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_mot_server=on')
        self.assertTrue(res)
        self.param_value = self.common.show_param(self.param)
        if 'off' not in self.param_value:
            res = self.pri_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, f'{self.param}=off')
            self.assertTrue(res, f'执行失败:{text}')
        self.pri_sh.restart_db_cluster()
        result = self.pri_sh.get_db_cluster_status('status')
        self.assertTrue(result, f'执行失败:{text}')
        current_value = self.common.show_param(self.param)
        self.assertTrue("off" in current_value, f'修改失败:{text}')

        text = "-----step2:创建mot表，字段类型包含xml;expect:成功-----"
        self.log.info(text)
        sql = f"drop foreign table if exists {self.table} cascade;" \
              f"create foreign table {self.table}" \
              f"(id int primary key, c_xml xml);"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn('ERROR:  Column definition of c_xml is not supported\n'
                      'DETAIL:  Column type xml is not supported yet', res,
                      f'执行create失败:{text}')

    def tearDown(self):
        text = '----step3:清理环境 expect:成功----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f'''drop foreign table if exists {self.table} cascade;
                CHECKPOINT;''')
        self.log.info(res)
        res_mot = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_mot_server=off')
        self.assertTrue(res_mot)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'{self.param} = {self.param_value}')
        self.pri_sh.restart_db_cluster()
        status = self.pri_sh.get_db_cluster_status('status')
        current_value = self.common.show_param(self.param)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, res,
                      f'执行失败:{text}')
        self.assertTrue(guc_res, f'执行失败:{text}')
        self.assertTrue(status, f'执行失败:{text}')
        self.assertTrue(self.param_value in current_value, f'执行失败:{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end------')
